﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;
using QLChoDiaOc.Forms;

namespace QLChoDiaOc
{
    /// <summary>
    /// Interaction logic for DangKyDiaOc.xaml
    /// </summary>
    public partial class DangKyDiaOc : Page
    {
        private InsertInfo _insertInfo = new InsertInfo();
        private SelectInfo _selectInfo = new SelectInfo();
        private DeleteInfo _deleteInfo = new DeleteInfo();
        private OtherHandle _otherHandle = new OtherHandle();
        private GetID _getId = new GetID();

        public DangKyDiaOc()
        {
            InitializeComponent();
            maKHTextBox.Text = App.KhachHangTemp.MaKH; //App.Makhachhang.ToString();
            if (App.IsDangKyDiaOc == false)
                maDiaOcTextBox.Text = _getId.GenerateMaDiaOc();
            HienThiThongTin();
            LoaiDiaOcComboBox.Items.Clear();
            for (int i = 0; i < _selectInfo.SelectAllTenLoaiDiaOc().Count; i++)
                LoaiDiaOcComboBox.Items.Add(_selectInfo.SelectAllTenLoaiDiaOc()[i]);
        }

        private void ResetAll()
        {
            //btnMaDiaOcAuto.IsEnabled = true;

            soNhaTextBox.Text = string.Empty;
            duongTextBox.Text = string.Empty;
            quanTextBox.Text = string.Empty;
            dienTichDatTextBox.Text = string.Empty;
            dienTichSuDungTextBox.Text = string.Empty;
            viTriTextBox.Text = string.Empty;
            huongNhaComboBox.SelectedItem = null;
            moTaTextBox.Text = string.Empty;
            huongNhaComboBox.Text = "";

            LoaiDiaOcComboBox.SelectedItem = null;

            //maDiaOcTextBox.IsEnabled = false;
            //soNhaTextBox.IsEnabled = false;
            //phuongTextBox.IsEnabled = false;
            //quanTextBox.IsEnabled = false;
            //dienTichDatTextBox.IsEnabled = false;
            //dienTichSuDungTextBox.IsEnabled = false;
            //viTriTextBox.IsEnabled = false;
            moTaTextBox.Text = string.Empty;
            LoaiDiaOcComboBox.SelectedItem = null;

            App.IsDangKyDiaOc = false;
        }

        private void HienThiThongTin()
        {
            if (App.IsDangKyDiaOc == true)
            {
                maDiaOcTextBox.Text = App.DiaOcTemp.MaDiaOc;
                viTriTextBox.Text = App.DiaOcTemp.ViTri;
                LoaiDiaOcComboBox.SelectedItem = _selectInfo.SelectTenLoaiDiaOc(App.DiaOcTemp.MaLoaiDiaOc);
                soNhaTextBox.Text = App.DiaOcTemp.SoNha;
                duongTextBox.Text = App.DiaOcTemp.Duong;
                quanTextBox.Text = App.DiaOcTemp.Quan;
                dienTichDatTextBox.Text = App.DiaOcTemp.DienTichDat.ToString();
                dienTichSuDungTextBox.Text = App.DiaOcTemp.DienTichSuDung.ToString();
                
                huongNhaComboBox.Text = App.DiaOcTemp.HuongNha;
                moTaTextBox.Text = App.DiaOcTemp.MoTa;
                giaBantextBox.Text = App.DiaOcTemp.GiaBan;

                viTriTextBox.IsEnabled = false;
                LoaiDiaOcComboBox.IsEnabled = false;
                soNhaTextBox.IsEnabled = false;
                duongTextBox.IsEnabled = false;
                quanTextBox.IsEnabled = false;
                dienTichDatTextBox.IsEnabled = false;
                dienTichSuDungTextBox.IsEnabled = false;
                huongNhaComboBox.IsEnabled = false;
                moTaTextBox.IsEnabled = false;
                giaBantextBox.IsEnabled = false;
                btnThem.Visibility = Visibility.Collapsed;
            }
        }

        //private void btnMaDiaOcAuto_Click(object sender, RoutedEventArgs e)
        //{
        //    maDiaOcTextBox.Text = _getId.GenerateMaDiaOc();

        //    //btnMaDiaOcAuto.IsEnabled = false;

        //    LoaiDiaOcComboBox.IsEnabled = true;
        //    soNhaTextBox.IsEnabled = true;
        //    phuongTextBox.IsEnabled = true;
        //    quanTextBox.IsEnabled = true;
        //    dienTichDatTextBox.IsEnabled = true;
        //    dienTichSuDungTextBox.IsEnabled = true;
        //    viTriTextBox.IsEnabled = true;
        //    huongNhaTextBox.IsEnabled = true;
        //    moTaTextBox.IsEnabled = true;
        //    giaBantextBox.IsEnabled = true;

        //    LoaiDiaOcComboBox.Items.Clear();
        //    for (int i = 0; i < _selectInfo.SelectAllTenLoaiDiaOc().Count; i++)
        //        LoaiDiaOcComboBox.Items.Add(_selectInfo.SelectAllTenLoaiDiaOc()[i]);
        //}

        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            //if (maKHTextBox.Text == string.Empty)
            //    MessageBox.Show("Nhấn vào nút Cấp Mã khách hàng để nhận mã tự động", "Thông Báo");
            //else
            //{
            if (LoaiDiaOcComboBox.SelectedItem == null
                || soNhaTextBox.Text == ""
                || duongTextBox.Text == ""
                || quanTextBox.Text == ""
                || dienTichDatTextBox.Text == ""
                || dienTichSuDungTextBox.Text == ""
                || viTriTextBox.Text == ""
                || huongNhaComboBox.Text == "")
                MessageBox.Show("Bạn cần nhập đủ thông tin", "Thông Báo");

            else
            {
                if (moTaTextBox.Text.ToArray().Count() > (int)_selectInfo.SelectThamSo("SoTuToiDa"))
                    MessageBox.Show("Số Ký Tự Mô Tả Quá Dài!", "Thông Báo");
                else
                {
                    try
                    {
                        App.DiaOcTemp.MaDiaOc = maDiaOcTextBox.Text;
                        App.DiaOcTemp.MaKH = maKHTextBox.Text;
                        App.DiaOcTemp.MaLoaiDiaOc = _selectInfo.SelectMaLoaiDiaOc(LoaiDiaOcComboBox.SelectedItem.ToString());
                        App.DiaOcTemp.SoNha = soNhaTextBox.Text;
                        App.DiaOcTemp.Duong = duongTextBox.Text;
                        App.DiaOcTemp.Quan = quanTextBox.Text;
                        App.DiaOcTemp.DienTichDat = float.Parse(dienTichDatTextBox.Text);
                        App.DiaOcTemp.DienTichSuDung = float.Parse(dienTichSuDungTextBox.Text);
                        App.DiaOcTemp.ViTri = viTriTextBox.Text;
                        App.DiaOcTemp.HuongNha = huongNhaComboBox.Text;
                        App.DiaOcTemp.MoTa = moTaTextBox.Text;
                        App.DiaOcTemp.GiaBan = giaBantextBox.Text;
                        App.DiaOcTemp.TinhTrang = 0;
                        MessageBox.Show("Thông tin hợp lệ", "Thông Báo");
                        App.Mainform.btnDangKyDichVu.IsEnabled = true;
                        App.IsDangKyDiaOc = true;

                        viTriTextBox.IsEnabled = false;
                        LoaiDiaOcComboBox.IsEnabled = false;
                        soNhaTextBox.IsEnabled = false;
                        duongTextBox.IsEnabled = false;
                        quanTextBox.IsEnabled = false;
                        dienTichDatTextBox.IsEnabled = false;
                        dienTichSuDungTextBox.IsEnabled = false;
                        huongNhaComboBox.IsEnabled = false;
                        moTaTextBox.IsEnabled = false;
                        giaBantextBox.IsEnabled = false;
                        btnThem.IsEnabled = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Thông Báo");
                    }
                }
            }
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            ResetAll();
            App.Mainform.btnDangKyDichVu.IsEnabled = false;
            App.Mainform.btnXacNhanGiayTo.IsEnabled = false;
            App.Mainform.btnLapPhieuThu.IsEnabled = false;
            App.IsDangKyDiaOc = false;

            //Huy QC
            App.IsDangKyQuangCao = false;
        }

        private void giaBantextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(giaBantextBox);
        }

        private void dienTichDatTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(dienTichDatTextBox);
        }

        private void dienTichSuDungTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(dienTichSuDungTextBox);
        }
    }
}